<!--  #微信授权 H5 -->
<script>
import { getUrlParam } from "@/utils";
import Cookies from "js-cookie"
import { useRouter } from "vue-router"
import { onBeforeMount } from "vue";
import { wxAuthLogin } from "@/api/member";
import { Toast } from "vant"
export default {
  setup() {
    const router = useRouter()
    onBeforeMount(() => {
      const code = getUrlParam("code");
      let state = getUrlParam("state");
      if (state==="undefined") state="STATE";
      if (code) {
         wxAuthLogin({params: { code: code,state: state }}).then(({ data: res}) => {
          if (res.code !== 0) {
            return Toast.fail(res.msg);
          }
          Cookies.set("token", res.data.token);
          setTimeout(()=> {
            router.replace("/");
          }, 100)
        })
      }else {
        Toast.fail("请在微信页面打开！");
        router.replace("login")
      }
    });
  },
}
</script>
